package com.cnswhy.cloud.server.merchant.controller.shop;

import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.server.core.dao.ShopClassifySpecMapper;
import com.cnswhy.cloud.server.core.dao.ShopSpecValueMapper;
import com.cnswhy.cloud.server.core.dto.ShopClassifySpecPageDTO;
import com.cnswhy.cloud.server.core.dto.ShopClassifySpecSaveDTO;
import com.cnswhy.cloud.server.core.dto.ShopClassifySpecUpdateDTO;
import com.cnswhy.cloud.server.core.entity.ShopClassifySpec;
import com.cnswhy.cloud.server.core.entity.ShopClassifySpecQuery;
import com.cnswhy.cloud.server.core.service.ShopClassifySpecService;
import com.cnswhy.cloud.server.core.service.ShopSpecValueService;
import com.cnswhy.cloud.server.merchant.dto.ShopClassifySpecQueryDTO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 商品分类与规格关系表
 * </p>
 *
 * @author cnswhy
 * @date 2020-11-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/shopClassifySpec")
@Api(value = "ShopClassifySpec", tags = "商品分类与规格关系表")
//@PreAuth(replace = "shopClassifySpec:")
public class ShopClassifySpecController extends SuperController<ShopClassifySpecService,Long , ShopClassifySpec, ShopClassifySpecPageDTO, ShopClassifySpecSaveDTO, ShopClassifySpecUpdateDTO> {
    @Autowired
    ShopClassifySpecMapper shopClassifySpecMapper;
    @Autowired
    ShopSpecValueService shopSpecValueService;
    @Autowired
    ShopSpecValueMapper shopSpecValueMapper;
    @PostMapping("/querySpecList")
    public R<List<ShopClassifySpecQuery>> querySpecList(@RequestBody ShopClassifySpecQueryDTO shopClassifySpecQueryDTO) {
        if(shopClassifySpecQueryDTO.getProductId() == null){
            shopClassifySpecQueryDTO.setProductId(-1L);
        }
        List<ShopClassifySpecQuery> shopClassifySpecQueryList = shopClassifySpecMapper.querySpecListByClassify(shopClassifySpecQueryDTO.getProductId(),shopClassifySpecQueryDTO.getClassifyId());
        for(var var : shopClassifySpecQueryList){
            if(shopClassifySpecQueryDTO.getProductId() != -1){
                var.setShopSpecValueList(shopSpecValueMapper.querySpecValueList(var.getProductId(),var.getId()));
            }else{
                var.setShopSpecValueList(shopSpecValueMapper.querySpecValueListBySpecId(var.getId()));
            }

        }
        return success(shopClassifySpecQueryList);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ShopClassifySpec> shopClassifySpecList = list.stream().map((map) -> {
            ShopClassifySpec shopClassifySpec = ShopClassifySpec.builder().build();
            return shopClassifySpec;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(shopClassifySpecList));
    }
}
